'use strict'

const router = require('koa-router')();
const getFiles = require('../tool/filters')


function registryRoute(router, routerFiles) {
    routerFiles.forEach(item => {
        let tmpPath = __dirname + '/' + item;
        let tmpObj = require(tmpPath);
        for (const key in tmpObj) {
            let tmpArr = key.split(' ');
            let rMethod = tmpArr[0];
            let rPath = tmpArr[1];
            if (rMethod === 'get') {
                router.get(rPath, tmpObj[key]);
            } else if (rMethod === 'post') {
                router.post(rPath, tmpObj[key]);
            } else if (rMethod === 'put') {
                router.put(rPath, tmpObj[key]);
            } else if (rMethod === 'delete') {
                router.delete(rPath, tmpObj[key]);
            } else {
                console.log('路径错误！');
            }
        }
    });
    return router.routes();
}


module.exports = () => {

    let routerFiles = getFiles(__dirname);

    let fn = registryRoute(router,routerFiles);

    return fn;
}